import { fabric } from 'fabric'

export function useTextOperations(getCanvas) {
  // 添加文字
  const addText = (options = {}) => {
    const fabricCanvas = getCanvas()
    if (!fabricCanvas) return

    const text = new fabric.IText('双击编辑文字', {
      left: 100,
      top: 100,
      fontSize: 20,
      fill: '#000000',
      ...options
    })
    fabricCanvas.add(text)
    fabricCanvas.setActiveObject(text)
    fabricCanvas.renderAll()
  }

  // 修改文字颜色
  const changeTextColor = (color) => {
    const fabricCanvas = getCanvas()
    if (!fabricCanvas) return

    const activeObject = fabricCanvas.getActiveObject()
    if (activeObject && activeObject.type === 'i-text') {
      activeObject.set('fill', color)
      fabricCanvas.renderAll()
    }
  }

  // 修改文字大小
  const changeTextSize = (size) => {
    const fabricCanvas = getCanvas()
    if (!fabricCanvas) return

    const activeObject = fabricCanvas.getActiveObject()
    if (activeObject && activeObject.type === 'i-text') {
      activeObject.set('fontSize', size)
      fabricCanvas.renderAll()
    }
  }

  return {
    addText,
    changeTextColor,
    changeTextSize
  }
} 